package com.glory.good_dark.activity;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.good_dark.R;
import com.glory.good_dark.application.App;
import com.glory.good_dark.application.BaseActivity;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_InitUtils;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_TextCallback;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_SharePreferencesUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author yi.zhang
 * @date 2019/6/13
 * @desc 设置界面
 */
public class SettingActivity extends BaseActivity {
    @BindView(R.id.webAddress_Txt)
    TextView webAddressTxt;
    @BindView(R.id.version_Txt)
    TextView versionTxt;
    @BindView(R.id.webAddress_Lay)
    LinearLayout webAddress_Lay;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.updataAddress_Txt)
    TextView updataAddressTxt;
    @BindView(R.id.timeOut_Lay)
    LinearLayout timeOut_Lay;
    @BindView(R.id.timeOut_Txt)
    TextView timeOut_Txt;

    private FreeUI_GeneralFragmentDialog dialog;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initView() {
        webAddressTxt.setText(App.WEB_URL);
        updataAddressTxt.setText(App.UPDATA_URL);
        timeOut_Txt.setText(String.valueOf(App.TIME_OUT));
        PackageManager manager = getPackageManager();
        PackageInfo info = null;
        try {
            info = manager.getPackageInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        String version = info.versionName;
        versionTxt.setText(version);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("设置");
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }

    @OnClick({R.id.webAddress_Lay,R.id.updataAddress_Lay,R.id.timeOut_Lay})
    public void onViewClicked(View view) {
        if(view.getId()==R.id.webAddress_Lay)
        {
            dialog = FreeApi_DialogUtils.getScanDialog(this, "请输入服务器地址",App.WEB_URL, new FreeUI_TextCallback() {
                @Override
                public void onSuccess(String s) {
                    webAddressTxt.setText(s);
                    App.WEB_URL = s;
                    showToast("保存成功", true);
                    FreeApi_SharePreferencesUtils.setSharePre("GOODARK_FILE", "WEB_URL", s, App.app);
                    dialog.dismiss();
                }
            });
            dialog.show(getSupportFragmentManager(), "WEB_URL_DIALOG");
        }
        if(view.getId()==R.id.updataAddress_Lay)
        {
            dialog = FreeApi_DialogUtils.getScanDialog(this, "请输入文件地址",App.UPDATA_URL, new FreeUI_TextCallback() {
                @Override
                public void onSuccess(String s) {
                    updataAddressTxt.setText(s);
                    App.UPDATA_URL = s;
                    showToast("保存成功", true);
                    FreeApi_SharePreferencesUtils.setSharePre("GOODARK_FILE", "UPDATA_URL", s, App.app);
                    dialog.dismiss();
                }
            });
            dialog.show(getSupportFragmentManager(), "WEB_URL_DIALOG");
        }
        if(view.getId()==R.id.timeOut_Lay)
        {
            dialog = FreeApi_DialogUtils.getScanDialog(this, "请输入响应时间(秒)",String.valueOf(App.TIME_OUT), new FreeUI_TextCallback() {
                @Override
                public void onSuccess(String s) {
                    try {
                        int timeout = Integer.parseInt(s);
                        timeOut_Txt.setText(s);
                        App.TIME_OUT = timeout;
                        FreeUI_InitUtils.initOkGo(App.getInstance(),timeout * 1000,0);
                        showToast("保存成功", true);
                        FreeApi_SharePreferencesUtils.setSharePre("GOODARK_FILE", "TIME_OUT", timeout, App.app);
                        dialog.dismiss();
                    }catch (NumberFormatException e){
                        showToast("请输入数字", false);
                    }

                }
            });
            dialog.show(getSupportFragmentManager(), "WEB_URL_DIALOG");
        }

    }
}
